﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MusicStore.Models;
using MusicStore.Repo;

namespace MusicStore.Repo.Test.Unit
{
    [TestClass]
    public class MusicStoreRepoTest
    {
        public MusicStoreRepoEntities repo = new MusicStoreRepoEntities();
        
        [TestInitialize]
        public void Setup()
        {
            repo.Setup();
        }

        [TestMethod]
        public void GetAlbumsReturns246Albums()
        {
            IList<Album> albums = repo.GetAllAlbums();
            Assert.AreEqual(246,albums.Count);
        }

        [TestMethod]
        public void PlaceOrderUpdateSalesAmount()
        {
            decimal salesAmount = repo.GetSalesAmount();
            var order = CreateOrder(string.Empty,33.4m);
            repo.PlaceOrder(order);
            
            Assert.AreEqual(salesAmount + order.Total, repo.GetSalesAmount());
        }

        private static Order CreateOrder(string username, decimal amount)
        {
            Order order = new Order();
            order.OrderDate = DateTime.Now;
            order.Username = username;
            order.Total = amount;
            return order;
        }
        private static Order CreateOrder()
        {
            Order order = new Order();
            order.OrderDate = DateTime.Now;
            order.Total = 33.4m;
            return order;
        }

        [TestMethod]
        public void IsValidChecksIdentityNameAndId()
        {
            string username = "Luke";
            int savedOrderId = repo.PlaceOrder(CreateOrder(username,0m));
            Assert.IsTrue(repo.IsValidOrder(savedOrderId, username));
        }

        [TestMethod]
        public void GetTopSellingsAlbumsAlwaysReturnsXAlbums()
        {
            Assert.AreEqual(3,repo.GetTopSellingAlbums(3).Count);
        }

        [TestMethod]
        public void GetAlbumByIdReturnTheRightAlbum()
        {
            Assert.AreEqual("Surfing with the Alien (Remastered)", repo.GetAlbumById(42).Title);
        }
        
        [TestMethod]
        public void GetGenreWithAlbumLoadAlbumInfos()
        {
            Assert.IsNotNull( repo.GetGenreWithAlbums("Rock").Albums.Find(x => x.Title == "Surfing with the Alien (Remastered)"));
        }

        [TestMethod]
        public void GetAlbumsGenresAndArtistReturnsGenreAndArtistInfos()
        {
            Assert.IsNotNull(repo.GetAlbumsGenresAndArtist()[0].Artist);
        }

        [TestMethod]
        public void GetAllArtistReturnsXArtists()
        {
            Assert.AreEqual(137, repo.GetAllArtists().Count);
        }

        [TestMethod]
        public void AddAlbumSaveAlbum()
        {
            string title = "Vitalogy";
            Album newAlbum = new Album() {Title = title, Artist = new Artist(),Genre = new Genre()};
            int id = repo.AddAlbum(newAlbum);
            Assert.AreEqual(title, repo.GetAlbumById(id).Title);
        }

        [TestMethod]
        public void DeleteAlbumUpdatesAlbumCount()
        {
            repo.DeleteAlbum(42);
            Assert.AreEqual(245,repo.GetAllAlbums().Count);
        }
    }
}